var GPY=function () {
  var gpysb = {
    _MainViewId: "view1",
    _AssistViewId: "view2",
    _Device:[0,null,null] ,
    _Video: [0,null,null],
    _SubType:[[],[],[]]
  };
  // 主摄像头插件
  gpysb._MainViewPlugin = function () {
    return document.getElementById(gpysb._MainViewId);
  }
  // 父摄像头插件
  gpysb._AssistViewPlugin = function () {
    return document.getElementById(gpysb._AssistViewId);
  }

  gpysb._AddEvent = function (obj, name, func) {
    if (obj.attachEvent) {
      obj.attachEvent("on" + name, func);
    } else {
      obj.addEventListener(name, func, false);
    }
  }

  gpysb._ChangeSubTypeMain = function () {

  }

  // idx ： 1主设备，2辅设备
  gpysb._OpenVideo = function (controlView,idx,resolution,substype) {
    gpysb._closeVideo(controlView,idx);
    if (!gpysb._Device[idx])
      return;

    gpysb._Video[idx] = controlView.Device_CreateVideo(gpysb._Device[idx], resolution, substype);
    if (gpysb._Video[idx]) {
      controlView.View_SelectVideo(gpysb._Video[idx]);
      controlView.View_SetText("打开视频中，请等待...", 0);
    }
  }

  gpysb._closeVideo=function(controlView,idx,msg)
  {
    if (gpysb._Video[idx])
    {
      controlView.Video_Release(gpysb._Video[idx]);
      gpysb._Video[idx]= null;
      controlView.View_SetText(msg||"视频已关闭", 0);
    }
  }

  gpysb._getDeviceResolutionCountEx=function(controlView,dev, SelectType){
    var aResolution=[];
    var nResolution = controlView.Device_GetResolutionCountEx(dev, SelectType);
    for(var i = 0; i < nResolution; i++)
    {
      var width = controlView.Device_GetResolutionWidthEx(dev, SelectType, i);
      var heigth = controlView.Device_GetResolutionHeightEx(dev, SelectType, i);
      aResolution.push( {value:i,text:width.toString() + "*" + heigth.toString()});
    }
    return aResolution;
  }

  gpysb.initView = function (objMainView,objAssistView) {
    var controlView=objMainView||objAssistView;

    objMainView&&objMainView.Global_SetWindowName("view");
    objAssistView&&objAssistView.Global_SetWindowName("view");

    gpysb._AddEvent(controlView, 'DevChange', function (type, idx, dbt) {
      if (1 != type) {
        // 非视频设备，直接退出
        return;
      }

      if (1 == dbt)//设备到达
      {
        var deviceType = controlView.Global_GetEloamType(1, idx);
        var deviceIndex=(1 == deviceType?1:2);
        var currDevice=(1==deviceIndex?objMainView:objAssistView)

        if (null == gpysb._Device[deviceIndex]) {
          gpysb._Device[deviceIndex] = controlView.Global_CreateDevice(1, idx);
          if (gpysb._Device[deviceIndex] && currDevice) {
            // 获取子设备
            gpysb._SubType[deviceIndex] = [];
            var subType = controlView.Device_GetSubtype(gpysb._Device[deviceIndex]);
            if (subType & 1) {
              gpysb._SubType[deviceIndex].push({
                value: 1,
                text: "YUY2",
                resolutions: gpysb._getDeviceResolutionCountEx(controlView, gpysb._Device[deviceIndex], 1)
              });
            }
            if (subType & 2) {
              gpysb._SubType[deviceIndex].push({
                value: 2,
                text: "MJPG",
                resolutions: gpysb._getDeviceResolutionCountEx(controlView, gpysb._Device[deviceIndex], 2)
              });
            }
            if (subType & 4) {
              gpysb._SubType[deviceIndex].push({
                value: 4,
                text: "UYVY",
                resolutions: gpysb._getDeviceResolutionCountEx(controlView, gpysb._Device[deviceIndex], 4)
              });
            }
            // console.log(gpysb._SubType[deviceIndex])
            // 打开设备
            if (gpysb._SubType[deviceIndex].length > 0) {
              gpysb._OpenVideo(currDevice, deviceIndex,gpysb._SubType[deviceIndex][0].resolutions[0].value,gpysb._SubType[deviceIndex][0].value);
            }
          }
        }
      }
      else if (2 == dbt)//设备丢失
      {
        for(var deviceIndex=1; deviceIndex<=2; deviceIndex++)
        {
          if (gpysb._Device[deviceIndex])
          {
            if (controlView.Device_GetIndex(gpysb._Device[deviceIndex]) == idx)
            {
              gpysb._closeVideo(controlView,deviceIndex,"设备丢失")
              controlView.Device_Release(gpysb._Device[deviceIndex]);
              gpysb._Device[deviceIndex] = null;
            }
          }
        }
      }
    });
  }
  gpysb.openDevice=function(controlView)
  {
    var ret = controlView.Global_InitDevs();
    if (!ret) {
      alert("初始化失败");
    }
  }

  gpysb.closeDevice=function(controlView){
    controlView.Global_DeinitDevs();
  }

  gpysb.unloadAllView = function (objMainView,objAssistView) {

    if (objMainView && gpysb._Video[1]) {
      objMainView.View_SetText("", 0);
      objMainView.Video_Release(gpysb._Video[1]);
      gpysb._Video[1] = null;
    }

    if (objMainView && gpysb._Device[1]) {
      objMainView.Device_Release(gpysb._Device[1]);
      gpysb._Device[1] = null;
    }

    if (objAssistView && gpysb._Video[2]) {
      objAssistView.View_SetText("", 0);
      objAssistView.Video_Release(gpysb._Video[2]);
      gpysb._Video[2] = null;
    }

    if (objAssistView && gpysb._Device[2]) {
      objAssistView.Device_Release(gpysb._Device[2]);
      gpysb._Device[2] = null;
    }
  }

  var idCardDevice={
    _ViewId: "view1"
  }

  idCardDevice.getDeviceControl=function(){
    return document.getElementById(idCardDevice._ViewId);
  }
  idCardDevice._AddEvent = function (obj, name, func) {
    if (obj.attachEvent) {
      obj.attachEvent("on" + name, func);
    } else {
      obj.addEventListener(name, func, false);
    }
  }
  idCardDevice.initDevice=function(){
    // let deviceControl=idCardDevice.getDeviceControl();
    // idCardDevice._AddEvent(deviceControl, 'IdCard', function(ret)
    // {
    //   alert(99)
    //   if (1 == ret)
    //   {
    //     var str = "：";
    //
    //     for(var i = 0; i < 16; i++)
    //     {
    //       str +=deviceControl.Global_GetIdCardData(i + 1);
    //       str += ";";
    //     }
    //
    //     alert(str);
    //
    //     // var image = plugin().Global_GetIdCardImage(1);//1表示头像， 2表示正面， 3表示反面 ...
    //     // plugin().Image_Save(image, "C:\\idcard.jpg", 0);
    //     // plugin().Image_Release(image);
    //
    //   }
    // });
  }


  return {
    camera: {
      createImgFilePath:function () {
        var date = new Date();
        var yy = date.getFullYear().toString();
        var mm = (date.getMonth() + 1).toString();
        var dd = date.getDate().toString();
        var hh = date.getHours().toString();
        var nn = date.getMinutes().toString();
        var ss = date.getSeconds().toString();
        var mi = date.getMilliseconds().toString();
        return yy + mm + dd + hh + nn + ss + mi + ".jpg";
      },
      // 关闭预览
      closeVideo: function () {
        var objMainView = gpysb._MainViewPlugin();
        var objAssistView = gpysb._AssistViewPlugin();
        objMainView && gpysb._closeVideo(objMainView, 1);
        objAssistView && gpysb._closeVideo(objAssistView, 2);
      },
      // 打开预览
      openVideo: function () {
        var objMainView = gpysb._MainViewPlugin();
        var objAssistView = gpysb._AssistViewPlugin();
        objMainView && gpysb._OpenVideo(objMainView, 1, gpysb._SubType[1][0].resolutions[0].value, gpysb._SubType[1][0].value);
        objAssistView && gpysb._OpenVideo(objAssistView, 2, gpysb._SubType[2][0].resolutions[0].value, gpysb._SubType[2][0].value);
      },
      // 更改设备或分辨率
      changeSubType: function (deviceIndex, resolutionIndex, subStypeIndex) {
        var controlView = (deviceIndex == 1 ? gpysb._MainViewPlugin() : gpysb._AssistViewPlugin());
        if (gpysb._Device[deviceIndex]) {
          gpysb._closeVideo(controlView, deviceIndex);
          gpysb._OpenVideo(controlView, deviceIndex, resolutionIndex, subStypeIndex);
        }
      }
      ,
      // 设备初始化并打开预览
      load: function () {
        this.unload();
        var objMainView = gpysb._MainViewPlugin();
        var objAssistView = gpysb._AssistViewPlugin();
        gpysb.initView(objMainView, objAssistView);
        gpysb.openDevice(objMainView || objAssistView);
      },
      // 关闭设备
      unload: function () {
        var objMainView = gpysb._MainViewPlugin();
        var objAssistView = gpysb._AssistViewPlugin();
        gpysb.unloadAllView(objMainView, objAssistView);
        gpysb.closeDevice(objMainView || objAssistView);
      },
      subTypes: function () {
        var res = [];
        for (var i = 1; i < gpysb._SubType.length; i++) {
          res.push(gpysb._SubType[i]);
        }
        return res;
      },
      createImage: function (deviceIndex, filepath) {
        var controlView = (deviceIndex == 1 ? gpysb._MainViewPlugin() : gpysb._AssistViewPlugin());
        var img = controlView.Video_CreateImage(gpysb._Video[deviceIndex], 0, controlView.View_GetObject());
        var fileName = (filepath ? filepath.trim("\\") + "\\" : "C:\\temp\\") +"idCard_"+ this.createImgFilePath();
        var bSave = controlView.Image_Save(img, fileName, 0);
        if (bSave) {
          controlView.View_PlayCaptureEffect();
        }
        controlView.Image_Release(img);

        return bSave ? fileName : "";
      },
      createImageBase64:function (deviceIndex) {
        var controlView = (deviceIndex == 1 ? gpysb._MainViewPlugin() : gpysb._AssistViewPlugin());
        var img = controlView.Video_CreateImage(gpysb._Video[deviceIndex], 0, controlView.View_GetObject());
        return controlView.Image_GetBase64(img, 2, 0);
      }
    },
    idCard: {
      createImgFilePath:function () {
        var date = new Date();
        var yy = date.getFullYear().toString();
        var mm = (date.getMonth() + 1).toString();
        var dd = date.getDate().toString();
        var hh = date.getHours().toString();
        var nn = date.getMinutes().toString();
        var ss = date.getSeconds().toString();
        var mi = date.getMilliseconds().toString();
        return yy + mm + dd + hh + nn + ss + mi + ".jpg";
      },
      closeIdCard: function () {
        var deviceControl = idCardDevice.getDeviceControl();
        deviceControl.Global_StopIdCardDiscern();
        deviceControl.Global_DeinitIdCard();
      },
      readIdCard: function (filepath) {
        this.closeIdCard();

        var resMsg = {
          imgpath1: "",
          imgpath2: "",
          imgpath3: "",
          name: "", // 姓名
          sex: "",  // 性别
          nation:"",  // 民族
          birthDate:"", // 出生日期
          idCardNo:"", // 身份证号码
          organization:"", // 签发机关
          validityCertificateDate:"" // 证件有效期
        }
        var deviceControl = idCardDevice.getDeviceControl();
        idCardDevice.initDevice();
        if (deviceControl.Global_InitIdCard()) {
          var ret = deviceControl.Global_ReadIdCard();
            if(ret)
            {
              resMsg.name=deviceControl.Global_GetIdCardData(1);//姓名
              resMsg.sex=deviceControl.Global_GetIdCardData(2);//性别
              resMsg.nation=deviceControl.Global_GetIdCardData(3);//民族
              resMsg.birthDate=deviceControl.Global_GetIdCardData(4)+"-"+deviceControl.Global_GetIdCardData(5)+"-"+deviceControl.Global_GetIdCardData(6); // 出生日期
              resMsg.address=deviceControl.Global_GetIdCardData(7); // 地址
              resMsg.idCardNo=deviceControl.Global_GetIdCardData(8); // 身份证号码
              resMsg.organization=deviceControl.Global_GetIdCardData(9); // 签发机关
              resMsg.validityCertificateDate=deviceControl.Global_GetIdCardData(10)+"-"+deviceControl.Global_GetIdCardData(11)+"-"+deviceControl.Global_GetIdCardData(12)+"至"+deviceControl.Global_GetIdCardData(13)+"-"+deviceControl.Global_GetIdCardData(14)+"-"+deviceControl.Global_GetIdCardData(15);; // 证件有效期
              var image = deviceControl.Global_GetIdCardImage(1);//1表示头像， 2表示正面， 3表示反面 ...
              var fileName = (filepath ? filepath.trim("\\") + "\\" : "C:\\temp\\") + this.createImgFilePath();
              resMsg.imgpath1=fileName;
              deviceControl.Image_Save(image, resMsg.imgpath1, 0);
              deviceControl.Image_Release(image);
            }
            else
            {
              return null;
            }
            deviceControl.Global_DeinitIdCard();
            return resMsg

        } else {
          alert("初始化失败")
          return null;
        }
      }
    }
  }
}();

export default GPY;
